var stand : boolean = true;
var isClimbing : boolean = false;
public static var animationJump : boolean = false;

var speed : float = 4;
var runSeconds : float = 10.0;

var standToRight : Texture2D;
var standToLeft : Texture2D;
var jumping01Right : Texture2D;
var jumping02Right : Texture2D;
var jumping01Left : Texture2D;
var jumping02Left : Texture2D;

var runToRight : Texture2D[];
var runToLeft : Texture2D[];
var climb : Texture2D[];

var hp : int = 100;
var timer : float = 0.0;
var acertou : boolean = false;
var StartDecrease : boolean = false;

function Start () 
{
	//transform.localScale -= Vector3(3.2,0,0);
}

function Update () 
{
    if(StartDecrease == true)
    {
       if(timer > 5)
       {
         hp -= 5;
         timer = 0.0;
       }   
    }
    
    print(hp);
    
    timer += Time.deltaTime;
    
	var translationH : float = Input.GetAxis("Horizontal") * speed;
	var translationV : float = Input.GetAxis("Vertical") * speed;
	
	translationH *= Time.deltaTime;
	translationV *= Time.deltaTime;
	transform.position.x += translationH;
	

	
	// Climb
	if(isClimbing)
	{
		transform.position.y += translationV;
		
		if(translationV != 0 | translationH != 0)
		{
			Climb();
		}
		collider.attachedRigidbody.useGravity = false;
		collider.attachedRigidbody.isKinematic = true;
	}
	else
	{
		collider.attachedRigidbody.useGravity = true;
		collider.attachedRigidbody.isKinematic = false;
	}
	
	// Stand
	if(stand && isClimbing == false)
	{
		renderer.material.mainTexture = standToRight;
	}
	
	// Stand
	if(stand == false && isClimbing == false)
	{
		renderer.material.mainTexture = standToLeft;
	}
	
	// Run
	if(translationH > 0 && isClimbing == false)
	{
		RunToRight();
		stand = true;
	}
	
	// Run
	if(translationH < 0 && isClimbing == false)
	{
		RunToLeft();
		stand = false;
	}
	
	// Jump
	if(Jump.canJump)
	{
		if(Input.GetKeyDown(KeyCode.Space))
		{
			rigidbody.AddForce(0,20000 * Time.deltaTime,0);
			animationJump = true;
		}
	}
	
	if(animationJump)
	{		
		if(rigidbody.velocity.y > 0)
		{
			if(stand)
			{
				renderer.material.mainTexture = jumping01Right;
			}
			
			if(stand == false)
			{
				
			}
		}
		
		if(rigidbody.velocity.y < 0)
		{
			if(stand)
			{
				renderer.material.mainTexture = jumping02Right;
			}
			
			if(stand == false)
			{
				
			}
		}
	}
	
	if(hp <= 0)
	{
	   Destroy(gameObject);
	}
}

function OnTriggerStay(other : Collider)
{
	if(other.gameObject.tag == "Fence")
	{
		isClimbing = true;
	}
}

function OnTriggerExit(other : Collider)
{
	if(other.gameObject.tag == "Fence")
	{
		isClimbing = false;
	}
}

function OnTriggerEnter(other : Collider)
{
	if(other.gameObject.tag == "Meteoro")
	{
		hp -= 20;
		Destroy(other.gameObject);
	}
	
	if(other.gameObject.tag == "Raio")
	{
		hp -= 40;
		Destroy(other.gameObject);
	}
	
	if(other.gameObject.tag == "Granizo")
	{
		hp -= 20;
		acertou = true;
		if(speed != 2 && timer > 5)
		{  
		  speed = 2;
		  acertou = false;
		  timer = 0.0;
		}
		Destroy(other.gameObject);
	}
	
	if(other.gameObject.tag == "Buraco")
	{
	   hp -= 100;
	}
	
	if(other.gameObject.tag == "Fantasma")
	{
		hp -= 50;
	}
	
	if(other.gameObject.tag == "Ras")
	{
		hp -= 30;
	}
	
	if(other.gameObject.tag == "Mumia")
	{
		hp -= 100;
	}
	
	if(other.gameObject.tag == "NuvemDeGafanhotos")
	{
		hp -= 20;
	}
	
	if(other.gameObject.tag == "MarDeSangue")
	{
		StartDecrease = true;
	}
}

function RunToRight()
{
	var indexRunToRight = Time.time * runSeconds;
	
	indexRunToRight = indexRunToRight % runToRight.Length;
	renderer.material.mainTexture = runToRight[indexRunToRight];
}

function RunToLeft()
{
	var indexRunToLeft = Time.time * runSeconds;
	
	indexRunToLeft = indexRunToLeft % runToLeft.Length;
	renderer.material.mainTexture = runToLeft[indexRunToLeft];
}

function Climb()
{
	var indexClimb = Time.time * runSeconds;
	
	indexClimb = indexClimb % climb.Length;
	renderer.material.mainTexture = climb[indexClimb];
}

function DecreaseSpeed (speed : float,new_speed : float, timer : float)
{
   if(timer > 5)
   {
      speed = new_speed;
      timer = 0.0f;
   }
}

